<template>
  <a-modal
      :title="`选择${title}`"
      v-model:visible="visible"
      width="840px"
      :afterClose="remove"
      @ok="handleOk"
  >
    <dynamic-table
        ref="tableRef"
        size="small"
        :columns="strategyColumns[strategyType]"
        :page-option="strategyTableProp[strategyType].pageOption"
        :get-list-func="strategyTableProp[strategyType].func"
        :rowKey="strategyTableProp[strategyType].rowKey"
        :row-selection="rowSelection" />
  </a-modal>
</template>

<script lang="ts">
import {defineComponent, reactive, toRefs, ref} from 'vue'
import {Modal} from 'ant-design-vue'
import DynamicTable from '@/components/dynamic-table.vue'
import {strategyTableProp} from './strategyTableProp'
import {strategyColumns} from './strategyColumns'

export default defineComponent({
  name: "select-modal",
  components: { [Modal.name]: Modal, DynamicTable},
  props: {
    remove: { // 移除模态框
      type: Function
    },
    title: {
      type: String
    },
    strategyType: {
     type: String,
     default: ''
    },
    callback: {
      type: Function
    }
  },
  setup(props) {
    const tableRef = ref<any>(null)

    const state = reactive({
      visible: true,
      data: [],
      uploading: false,
      tableLoading: false,
      rowSelection: {
        onChange: (selectedRowKeys, selectedRows) => {
          state.rowSelection.selectedRowKeys = selectedRowKeys
        },
        selectedRowKeys: [] as any[]
      },
    })

    const handleOk = () => {
      const rowKey = strategyTableProp[props.strategyType].rowKey
      const data = tableRef.value.data as object[]
      const selected = data.filter((item: any) => state.rowSelection.selectedRowKeys.includes(item[rowKey]))
      props.callback && props.callback(props.strategyType,selected) && props.remove && props.remove()
      state.visible = false
    }

    return {
      ...toRefs(state),
      handleOk,
      tableRef,
      strategyColumns,
      strategyTableProp
    }
  }
})
</script>

<style scoped>
</style>
